﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;

namespace HCPDemo.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult UploadHCPFile()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public string UploadHCPFile(HttpPostedFileBase file)
        {
            if (file == null)
            {
                return "文件不存在";
            }

            try
            {
                string targetPath = string.Format("/{0}", file.FileName);
                HCPFileService.WriteObject(targetPath, file);
                return file.FileName + "上传成功";

            }
            catch(Exception ex)
            {
                return ex.Message;
            }

           
        }

        [HttpGet]
        public ActionResult WriteMetadata()
        {
            return View();
        }


        [HttpPost]
        [ValidateAntiForgeryToken]
        public string WriteMetadata(string targetPath)
        {
            string result;
            try
            {
               
                if(!targetPath.StartsWith("/"))
                {
                    targetPath="/"+targetPath;
                }
                string  requestdata = "<FileMetadata><ProjectName>XXXX</ProjectName></FileMetadata>";
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(requestdata);
                HCPFileService.WriteObjectMetadata(targetPath, requestdata);
                result = "成功写入元数据:" + requestdata;
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return result;
        }

        [HttpGet]
        public ActionResult GetHCPFile(string targetPath)
        {
            if (!string.IsNullOrWhiteSpace(targetPath))
            {
                try
                {
                    if (!targetPath.StartsWith("/"))
                    {
                        targetPath = "/" + targetPath;
                    }

                    byte[] bytes = null;
                    using (var stream = new MemoryStream())
                    {
                        HCPFileService.ReadObjectToSream(stream, targetPath);
                        bytes = stream.ToArray();
                    }
                    return File(bytes, "application/x-zip-compressed", targetPath.Replace("/", ""));
                }
                catch (Exception ex)
                {
                    //return RedirectToAction("ObjectNotFound", "Common");
                    return View();
                }
            }

            return View();
        }


       
        [HttpGet]
        public ActionResult GetHCPMetadata(string targetPath)
        {

            string result="";
            if (!string.IsNullOrWhiteSpace(targetPath))
            {
                try
                {

                    if (!targetPath.StartsWith("/"))
                    {
                        targetPath = "/" + targetPath;
                    }

                    result = HCPFileService.ReadObjectMetadata(targetPath);
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
            }

            ViewBag.Metadata = result;

            return View();
        }

    }
}
